/**
 * 投标信息申请 - 编辑页面
 * @author miaoyl
 * @date 2018/01/14
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isDetail: false,
            isAddGwde: false,
            files: g_ctxPath + 'upload/files',
            file: '',
            fileList: [],
            address: '',
            count: 0,
            gwgc004Disabled:true,
            gwgc001show:true,
            gwgeBtnDisabled:true,
            saveBtnDisabled:true,
            AMOUNT: 100,
            form: {
                GWAK009:null,
                GWGCORIG_NAME: null,
                GWGCORIU_NAME: null,
                GWGA008_NAME: null,
                GWGC005_NAME: null,
                GWGC010_NAME: null,
                GWGC011_NAME: null,
                GWGC012_NAME: null,
                GWGC013_NAME: null,
                GWGC023_NAME: null,
                GWGC028_NAME: null,
                GWGC007_LIST: []
            },
            rules1: {
                GWGCORIU_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGCORIG_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGCDATE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGC001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGA002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGC002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGC003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGC004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGC022: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGC023: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGC024: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGC028_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
                // GWGC008: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ]
                // GWGC010_NAME: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // GWGC011_NAME: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // GWGC012_NAME: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ],
                // GWGC013_NAME: [
                //     {required: true, message: '不可为空', trigger: 'blur'}
                // ]
            },
            rules2: {
                GWGCORIU_NAME: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGCORIG_NAME: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGCDATE: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGC001: [
                    {required: false, message: '不可为空', trigger: 'change'}
                ],
                GWGA002: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGC002: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGC003: [
                    {required: false, message: '不可为空', trigger: 'change'}
                ],
                GWGC004: [
                    {required: false, message: '不可为空', trigger: 'change'}
                ],
                GWGC022: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGC023: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGC024: [
                    {required: false, message: '不可为空', trigger: 'blur'}
                ],
                GWGC028_NAME: [
                    {required: false, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_n_y: app.datas_('sys_n_y'),
                gwgc007Options: app.datas_('crm_gwgi002_gwgc007'),
                gwak009Options: app.datas_("crm_gwai300_gwak009")
            },

            uploadUrl: g_ctxPath + 'upload/files',
            basicInfo_file: '',
            basicInfo_fileList: [],
            basicInfo_fileList2: [],
            basicInfo_fileCount: 0,
        },
        cols_gwgd = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWGD002', width: 80, title: '序号'}
            , {field: 'GWGD003', width: 80, title: '标段号', edit:'text'}
            , {field: 'GWGD004', width: 100, title: '项目类型', edit:'select',
                filterDatas: [{code:'',value:'请选择'}].concat(app.datas_('crm_gwgi002_gwgd004'))
            }
            /* , {field: 'GWGD005', width: 140, title: '车辆型号', event:'openGwgd005'}*/
            , {field: 'GWGD006', width: 140, title: '产品编号', event:'openGwgd006'}
            , {field: 'GWAHL003', width: 140, title: '产品大类'}
            , {field: 'GWAOL003', width: 140, title: '车辆类型'}
            , {field: 'GWGD007', width: 100, title: '数量', edit: 'text'}
            , {field: 'GWGD008', width: 100, title: '预算单价(万元)', edit: 'text'}
            , {field: 'GWGD009', width: 100, title: '预算总计(万元)'}
            , {field: 'GWGD010', width: 100, title: '中标单价(万元)', edit: 'text'}
        ]],
        cols_gwge = [[
            {type: 'checkbox', title: '选择'}
            , {field: 'GWGE002', width: 80, title: '序号'}
            , {field: 'GWGE006', width: 100, title: '参与投标单位',edit: 'text'}
            , {field: 'GWGE003', width: 100, title: '标段号', edit:'text'}
            /*, {field: 'GWGE004', width: 120, title: '车辆型号', event:'openGwge004'}*/
            , {field: 'GWGE005', width: 140, title: '车型', event:'openGwge005'}
            /*, {field: 'GWAML004', width: 80, title: '规格'}*/
            , {field: 'GWAHL003', width: 80, title: '产品大类'}
            , {field: 'GWAOL003', width: 120, title: '车辆类型'}
            , {field: 'GWGE007', width: 100, title: '数量',edit: 'text'}
            , {field: 'GWGE008', width: 100, title: '单价(万元)',edit: 'text'}
            , {field: 'GWGE009', width: 100, title: '总计(万元)'}
            , {field: 'GWGE010', width: 100, title: '是否中标', edit:'select',
                filterDatas: app.datas_('sys_n_y')
            }
        ]],
        pageVue,
        table,
        callObj, layOpenIndex,
        gwgdTable, gwgeTable
    ;

    var Gwgi002Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwgi002_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: '/common/_open_window'
                }]
            }, win);
        },

        initGwgdTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwgdTable = table.render({
                    elem: app.getByid(doc, 'gwgi002_gwgd_table'),
                    id: 'gwgi002_gwgd_table',
                    cols: cols_gwgd,
                    height: '250',
                    page: false
                });

                //监听单元格编辑
                table.on('edit(gwgi002_gwgd_table)', function (obj) {
                    var oldValue = $(this).prev().text();
                    if(obj.field == 'GWGD007'){
                        var re = /(^[1-9]\d*$)/; //判断是否正整数
                        if (!re.test(obj.value)) {
                            obj.updateOfEdit({
                                GWGD007: oldValue
                            });
                            app.showWarning('数量必须为正整数！');
                            return;
                        }
                        if(obj.data.GWGD008 != ''){
                            obj.updateOfEdit({
                                GWGD009: self.calculate(math.mul(obj.data.GWGD008, obj.value), vueData.AMOUNT)
                            })
                        }
                    }else if(obj.field == 'GWGD008'){
                        var reg = /^\d+(\.\d{1,4})?$/;
                        if(!reg.test(obj.value)){
                            obj.updateOfEdit({
                                GWGD008: oldValue
                            });
                            app.showWarning('单价为正数且最多输四位小数！');
                            return;
                        }
                        if(obj.data.GWGD007 != ''){
                            obj.updateOfEdit({
                                GWGD009: self.calculate(math.mul(obj.data.GWGD007, obj.value), vueData.AMOUNT)
                            })
                        }
                    }else if(obj.field == 'GWGD010'){
                        var reg = /^\d+(\.\d{1,4})?$/;
                        if(obj.value != '' && !reg.test(obj.value)){
                            obj.updateOfEdit({
                                GWGD010: oldValue
                            });
                            app.showWarning('单价为正数且最多输四位小数！');
                            return;
                        }
                        var gwgdList = gwgdTable.getDatas();
                        let sum = 0;
                        gwgdList.forEach(function (item) {
                            if(item.GWGD007 != '' && item.GWGD007 != undefined && item.GWGD010 != '' && item.GWGD010 != undefined){
                                var total = math.mul(item.GWGD007, item.GWGD010);
                                sum = math.add(sum, total);
                            }
                        });
                        vueData.form.GWGC016 = sum;
                    }
                });

                table.on('tool(gwgi002_gwgd_table)', function (obj) {
                    if(! vueData.isAddGwde) {
                        if (obj.event == 'openGwgd005') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwaml003',
                                        columnList: [
                                            {label: '车辆型号', prop: 'GWAML003', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWGD005: data.GWAML003
                                        })
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择车辆型号',
                                    area: ['400px', '470px']
                                }
                            });
                        } else if (obj.event == 'openGwgd006') {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_gwam001',
                                        columnList: [
                                            {label: '产品编号', prop: 'GWAM001', width: '150'},
                                            {label: '规格', prop: 'GWAML004', width: '150'},
                                            {label: '产品大类', prop: 'GWAHL003', width: '150'},
                                            {label: '车辆类型', prop: 'GWAOL003', width: '150'}
                                        ],
                                        searchParam: {
                                            //GWAML003: obj.data.GWGD005
                                        }
                                    },
                                    cb: function (data) {
                                        obj.updateOfEdit({
                                            GWGD006: data.GWAM001,
                                            GWAML004: data.GWAML004,
                                            GWAHL003: data.GWAHL003,
                                            GWAOL003: data.GWAOL003,
                                            GWGD007: '',
                                            GWGD008: '',
                                            GWGD009: '',
                                            GWGD010: ''
                                        })
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择产品编号',
                                    area: ['700px', '470px']
                                }
                            });
                        }
                    }
                });
            });
        },

        initGwgeTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwgeTable = table.render({
                    elem: app.getByid(doc, 'gwgi002_gwge_table'),
                    id: 'gwgi002_gwge_table',
                    cols: cols_gwge,
                    height: '250',
                    page: false
                });

                //监听单元格编辑
                table.on('edit(gwgi002_gwge_table)', function (obj) {
                    var oldValue = $(this).prev().text();
                    if(obj.field == 'GWGE003'){ //标段号
                        console.log('oldValue',oldValue);
                        console.log('obj',obj.data);
                        if(obj.data.GWGE010 == 'Y'){
                            if(oldValue != ''){
                                self.getGwdc016ByGwgw013(obj.data, '', oldValue);
                                //self.getGwdc016(obj.data, obj.data.GWGE008);
                                //self.getGwdc016ByGwgw013(obj.data);
                            }else {
                                self.getGwdc016(obj.data, obj.data.GWGE008);
                            }
                        }
                    }else if(obj.field == 'GWGE007'){ //数量
                        var re = /(^[1-9]\d*$)/; //判断是否正整数
                        if (!re.test(obj.value)) {
                            obj.updateOfEdit({
                                GWGE007: oldValue
                            });
                            app.showWarning('数量必须为正整数！');
                            return;
                        }
                        if(obj.data.GWGE008 != ''){
                            obj.updateOfEdit({
                                GWGE009: self.calculate(math.mul(obj.data.GWGE008, obj.value), vueData.AMOUNT)
                            })
                        }
                    }else if(obj.field == 'GWGE008'){//单价
                        var reg = /^\d+(\.\d{1,4})?$/;
                        if(!reg.test(obj.value)){
                            obj.updateOfEdit({
                                GWGE008: oldValue
                            });
                            app.showWarning('单价为正数且最多输四位小数！');
                            return;
                        }
                        if(obj.data.GWGE010 == 'Y'){
                            self.getGwdc016(obj.data, obj.value);
                        }
                        if(obj.data.GWGE007 != ''){
                            obj.updateOfEdit({
                                GWGE009: self.calculate(math.mul(obj.data.GWGE007, obj.value), vueData.AMOUNT)
                            });
                        }
                    }else if(obj.field == 'GWGE010'){
                        console.log('oldValue',oldValue);
                        console.log('obj.value',obj.value);
                        var gwdeList = gwgeTable.getDatas();
                        if(obj.value == 'Y'){
                            var flag = true;
                            gwdeList.forEach(function (v) {
                                if(obj.data.GWGE003 == v.GWGE003 && obj.data.GWGE005 == v.GWGE005 && obj.data.GWGE002 != v.GWGE002 && v.GWGE010 == 'Y'){
                                    obj.updateOfEdit({
                                        GWGE010: 'N'
                                    });
                                    flag = false;
                                    app.showWarning("同一个标段号+产品编号只能有一笔中标！");
                                    return ;
                                }
                            });
                            if(flag) {
                                self.getGwdc016(obj.data, obj.data.GWGE008);
                            }
                        }else {
                            // if(oldValue != "否" && oldValue != 'N'){ // || oldValue == obj.value
                            //     self.getGwdc016(obj.data, '');
                            // }
                            self.getGwdc016By010N(obj.data);
                        }
                    }
                });

                table.on('tool(gwgi002_gwge_table)', function (obj) {
                    if (obj.event == 'openGwge004') {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwaml003',
                                    columnList: [
                                        {label: '车辆型号', prop: 'GWAML003', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    obj.updateOfEdit({
                                        GWGE004: data.GWAML003
                                    })
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择车辆型号',
                                area: ['400px', '470px']
                            }
                        });
                    } else if (obj.event == 'openGwge005') {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwam001',
                                    columnList: [
                                        {label: '产品编号', prop: 'GWAM001', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'},
                                        {label: '产品大类', prop: 'GWAHL003', width: '150'},
                                        {label: '车辆类型', prop: 'GWAOL003', width: '150'}
                                    ],
                                    searchParam: {
                                        /*GWAML003: obj.data.GWGE004*/
                                    }
                                },
                                cb: function (data) {
                                    if(obj.data.GWGE010 == 'Y') {
                                        self.getGwdc016(obj.data, '');
                                    }
                                    obj.updateOfEdit({
                                        GWGE005: data.GWAM001,
                                        /*GWAML004: data.GWAML004,*/
                                        GWAHL003: data.GWAHL003,
                                        GWAOL003: data.GWAOL003,
                                        GWGE007: '',
                                        GWGE008: '',
                                        GWGE009: ''
                                    });
                                    obj.update({
                                        GWGE010: 'N'
                                    })
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择车型',
                                area: ['700px', '470px']
                            }
                        });
                    }
                });
            });
        },

        getGwdc016By010N: function (rowData) { //为N清空
            var sum = 0;
            var flag = true;
            var gwgdList = gwgdTable.getDatas();
            var gwgeList = gwgeTable.getDatas();
            gwgeList.forEach(function (v) {//表格内有同一标段车型且中标数据
                if(rowData.GWGE003 == v.GWGE003 && rowData.GWGE005 == v.GWGE005 && rowData.GWGE002 != v.GWGE002 && v.GWGE010 == 'Y'){
                    flag = false;
                    return ;
                }
            });
            if(flag){
                gwgdList.forEach(function (item) {
                    if(rowData.GWGE003 == item.GWGD003 && rowData.GWGE005 == item.GWGD006){
                        item.GWGD010 = '';
                    }else {
                        if(item.GWGD007 != '' && item.GWGD007 != undefined && item.GWGD010 != '' && item.GWGD010 != undefined){
                            var total = math.mul(item.GWGD007, item.GWGD010);
                            sum = math.add(sum, total);
                        }
                    }
                });
                self.gwgdTableReload(gwgdList);
                if(sum == 0){
                    pageVue.form.GWGC016 = '';
                }else {
                    pageVue.form.GWGC016 = sum;
                }
            }
        },

        getGwdc016ByGwgw013:function (rowData, value, GWGE003) { //回写中标单价及计算中标总价
            var sum = 0;
            var gwgdList = gwgdTable.getDatas();
            gwgdList.forEach(function (item) {
                if(GWGE003 == item.GWGD003 && rowData.GWGE005 == item.GWGD006){
                    item.GWGD010 = value;
                    if(value != '' && item.GWGD007 != '' && item.GWGD007 != undefined){
                        var total = math.mul(item.GWGD007, value);
                        sum = math.add(sum, total);
                    }
                }else {
                    if(item.GWGD007 != '' && item.GWGD007 != undefined && item.GWGD010 != '' && item.GWGD010 != undefined){
                        var total = math.mul(item.GWGD007, item.GWGD010);
                        sum = math.add(sum, total);
                    }
                }
            });
            self.gwgdTableReload(gwgdList);
            if(sum == 0){
                pageVue.form.GWGC016 = '';
            }else {
                pageVue.form.GWGC016 = sum;
            }
            //console.log('getGwdc016ByGwgw013');
            self.getGwdc016(rowData, rowData.GWGE008);
        },

        getGwdc016:function (rowData, value) { //回写中标单价及计算中标总价
            var sum = 0;
            var gwgdList = gwgdTable.getDatas();
            gwgdList.forEach(function (item) {
                if(rowData.GWGE003 == item.GWGD003 && rowData.GWGE005 == item.GWGD006){
                    item.GWGD010 = value;
                    if(value != '' && item.GWGD007 != '' && item.GWGD007 != undefined){
                        var total = math.mul(item.GWGD007, value);
                        sum = math.add(sum, total);
                    }
                }else {
                    if(item.GWGD007 != '' && item.GWGD007 != undefined && item.GWGD010 != '' && item.GWGD010 != undefined){
                        var total = math.mul(item.GWGD007, item.GWGD010);
                        sum = math.add(sum, total);
                    }
                }
            });
            self.gwgdTableReload(gwgdList);
            if(sum == 0){
                pageVue.form.GWGC016 = '';
            }else {
                pageVue.form.GWGC016 = sum;
            }
        },

        editData: function (listData) {

            vueData.form.GWGC007 = vueData.form.GWGC007_LIST.join(";");
            app.ajax({
                xurl: 'crm/gwgi002/editData',
                xdata: {gwgdList:listData,gwgcData:vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: '投标申请信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        vueData.basicInfo_fileList= [];
                        vueData.basicInfo_fileList2 = [];
                        app.pageModule('gwgi002').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },

        addData: function (listData) {
            vueData.form.GWGC007 = vueData.form.GWGC007_LIST.join(";");
            app.ajax({
                xurl: 'crm/gwgi002/addData',
                xdata: {gwgdList:listData,gwgcData:vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: '投标申请信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        vueData.basicInfo_fileList= [];
                        vueData.basicInfo_fileList2 = [];
                        app.pageModule('gwgi002').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },

        addGwgeData: function (gwgdList, gwgeList) {
            app.ajax({
                xurl: 'crm/gwgi002/addGwgeData',
                xdata: {gwgdList:gwgdList, gwgeList:gwgeList,gwgcData: vueData.form},
                loading: true,
                toJsonStr: true,
                okmsg: '中标信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwgi002').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },

        calculate:function (num, digit) {
            var data = math.div(Math.round(math.mul(num, digit)),digit);
            return data;
        },

        _call: function (obj) {
            vueData.basicInfo_fileList= [];
            vueData.basicInfo_fileList2 = [];
            vueData.fileCount = 0;

            pageVue.$refs['form'].resetFields();
            layOpenIndex = obj.layOpenIndex;
            // vueData.form.GWGC007 = [];
            // vueData.fileList = [];
            // vueData.address = '';
            // vueData.count = 0;
            gwgdTable.config.cols[0][10].edit = false;
            if(obj.is_addGwde){//编辑中标信息
                vueData.isAddGwde = true;
                vueData.gwgc001show = true
                vueData.gwgc004Disabled = true;
                vueData.gwgeBtnDisabled = true;
                gwgdTable.config.cols[0][2].edit = false;
                gwgdTable.config.cols[0][3].edit = false;
                gwgdTable.config.cols[0][7].edit = false;
                gwgdTable.config.cols[0][8].edit = false;
                if(obj.obj.data.GWGC014 == 'Y'){
                    gwgdTable.config.cols[0][10].edit = 'text';
                    gwgeTable.config.cols[0][10].edit = false;
                }else{
                    gwgdTable.config.cols[0][10].edit = false;
                    gwgeTable.config.cols[0][10].edit = 'select';
                }
            }else {
                vueData.isAddGwde = false;
                vueData.gwgc001show = false;
                vueData.gwgeBtnDisabled = false;
                if(obj.obj != undefined && obj.obj.data.GWGC004 == 'Y'){
                    vueData.gwgc004Disabled = false;
                }else if(obj.obj != undefined && vueData.form.GWGC004 == 'N'){
                    vueData.gwgc004Disabled = true;
                }
                gwgdTable.config.cols[0][2].edit = 'text';
                gwgdTable.config.cols[0][3].edit = 'select';
                gwgdTable.config.cols[0][7].edit = 'text';
                gwgdTable.config.cols[0][8].edit = 'text';
            }
            if(obj.is_detail){
                vueData.isDetail = true;
                vueData.isAddGwde = true;
                vueData.gwgeBtnDisabled = true;
                vueData.saveBtnDisabled = true
                vueData.gwgc001show = true;
            }else {
                if(obj.is_addGwde){
                    vueData.isDetail = true;
                    vueData.gwgeBtnDisabled = false;
                    vueData.gwgc001show = true;
                }else {
                    vueData.isDetail = false;
                    vueData.gwgeBtnDisabled = true;
                    vueData.gwgc001show = false;
                }
                vueData.saveBtnDisabled = false;
            }
            if (obj.is_add) {
                vueData.isAdd = true;
                vueData.disabled = true;
                vueData.gwgc001show = false;
                app.ajax({
                    xurl: 'crm/gwgi002/getAddData',
                    xdata: {},
                    cb: function (result) {
                        if (result.ok) {
                            app.extendUpLow({
                                target: vueData.form,
                                res: [result.data.data,
                                    {
                                        GWGCACTI: 'N'
                                    }
                                ],
                                reset: true
                            });
                        }
                    }
                });
                self.gwgdTableReload([]);
                self.gwgeTableReload([]);
            } else {
                vueData.isAdd = false;
                vueData.gwgc001show = true;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });

                if (callObj.data.GWGC029 != undefined && callObj.data.GWGC029 != "") {
                    if (callObj.data.GWGC029.length > 0) {

                        let list = callObj.data.GWGC029.split(',');
                        for (let i = 0; i < list.length; i++) {
                            vueData.basicInfo_fileList.push({
                                name: list[i], url: '/wissun-smfile/' + list[i]
                            });
                            vueData.basicInfo_fileList2.push({GWGC029: list[i]});
                        }
                    }
                }

                vueData.form.GWGC007_LIST = callObj.data.GWGC007 == undefined ? []: callObj.data.GWGC007.split(";");
                // if (callObj.data.GWGC009 != undefined) {
                //     if (callObj.data.GWGC009.length > 0) {
                //         vueData.address = callObj.data.GWGC009;
                //         vueData.fileList.push({
                //             name: callObj.data.GWGC009, url: '/smfile/' + callObj.data.GWGC009
                //         })
                //     }
                // }
                app.ajax({
                    xurl: 'crm/gwgi002/getTable',
                    xdata: {GWGCID: callObj.data.GWGCID},
                    cb: function (result) {
                        if (result.ok) {
                            var gwgdList = result.data.data.gwgdList;
                            var gwgeList = result.data.data.gwgeList;
                            self.gwgdTableReload(gwgdList);
                            self.gwgeTableReload(gwgeList);
                        }
                    }
                });
            }
            pageVue.$nextTick(function(){  //使用vue.$nextTick()方法可以dom数据更新后延迟执行
                pageVue.$refs['form'].clearValidate();
            });
        },

        gwgdTableReload: function (listData1) {
            self._reloadTable({id: 'gwgi002_gwgd_table', data: listData1});
        },

        gwgeTableReload: function (listData2) {
            self._reloadTable({id: 'gwgi002_gwge_table', data: listData2});
        },

        compareDate:function(date1,date2) {
            if(date1 == null || date1 === "" || date2 == null || date2 === ""){
                return false;
            }
            let d1 = new Date(date1);
            let d2 = new Date(date2);
            return d1.getTime() < d2.getTime();
        },
        _ready: function () {
            vueData.form = app.fields_(['GWGC_T'], vueData.form);
            pageVue = new Vue({
                el: '#gwgi002_edit',
                data: vueData,
                methods: {
                    openGwgc001: function () {//招标编号开窗
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwga001',
                                    columnList: [
                                        {label: '招标编号', prop: 'GWGA001', width: '150'},
                                        {label: '项目名称', prop: 'GWGA002', width: '150'},
                                        // {label: '客户', prop: 'GWAKL004', width: '150'},
                                        // {label: '开标时间', prop: 'GWAKL004', width: '150'}
                                    ],
                                    searchParam:{
                                        GWGAACTI: 'Y'
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWGC001 = data.GWGA001;
                                    // 改为手动输入，不从招标编号带出
                                    // vueData.form.GWGA002 = data.GWGA002;
                                    // vueData.form.GWGA008_NAME = data.GWAKL004;
                                    // vueData.form.GWGA006 = data.GWGA006;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择招标信息',
                                area: ['600px', '465px']
                            }
                        });
                    },

                    openGwgc023:function() { // 客户
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '名称', prop: 'GWAKL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC023 = data.GWAK001;
                                    vueData.form.GWGC023_NAME = data.GWAKL004;
                                    vueData.form.GWAK009 = data.GWAK009;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择客户',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwgc005: function () {//授权代理商
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwak001',
                                    searchParam:{
                                        GWAK006 : "2"
                                    },
                                    columnList: [
                                        {label: '代理商编号', prop: 'GWAK001', width: '150'},
                                        {label: '名称', prop: 'GWAKL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC005 = data.GWAK001;
                                    vueData.form.GWGC005_NAME = data.GWAKL004;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择授权代理商',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwgc010:function() {//商务制作员
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [
                                        {label: '账号', prop: 'ACCOUNT', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC010 = data.ID;
                                    vueData.form.GWGC010_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择商务制作员',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwgc011: function () {//商务审核员
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysuser_id',
                                    columnList: [
                                        {label: '账号', prop: 'ACCOUNT', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC011 = data.ID;
                                    vueData.form.GWGC011_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择商务审核员',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGwgc012: function () {//技术制作员
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [
                                        {label: '账号', prop: 'ACCOUNT', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC012 = data.ID;
                                    vueData.form.GWGC012_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择技术制作员',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwgc013: function () {//技术审核员
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [
                                        {label: '账号', prop: 'ACCOUNT', width: '150'},
                                        {label: '姓名', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC013 = data.ID;
                                    vueData.form.GWGC013_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择技术审核员',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGwgc028:function() {//投标单位
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysorg_biz',
                                    columnList: [
                                        {label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '名称', prop: 'NAME', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGC028 = data.ID;
                                    vueData.form.GWGC028_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择投标单位',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if(!vueData.isAddGwde) {
                                    var flag_list = true;
                                    if(vueData.form.GWGC004 == 'Y'){
                                        if(vueData.form.GWGC005 == '' || vueData.form.GWGC005 == null){
                                            app.showWarning("授权代理商必填！");
                                            return;
                                        }
                                        if(vueData.form.GWGC006 == '' || vueData.form.GWGC006 == null){
                                            app.showWarning("是否新增代理商必填！");
                                            return;
                                        }
                                    }
                                    var listData = gwgdTable.getDatas();
                                    if(listData.length < 1){
                                        app.showWarning('投标车型明细数据不能为空！');
                                        return;
                                    }else {
                                        listData.forEach(function (v) {
                                            if(v.GWGD006 === null || v.GWGD006 === '' || v.GWGD006 === undefined){
                                                app.showWarning('产品编号不能为空！');
                                                flag_list = false;
                                                return;
                                            }
                                            if(v.GWGD003 === null || v.GWGD003 === '' || v.GWGD003 === undefined){
                                                app.showWarning('标段号不能为空！');
                                                flag_list = false;
                                                return;
                                            }
                                            if(v.GWGD004 === null || v.GWGD004 === '' || v.GWGD004 === undefined){
                                                app.showWarning('项目类型不能为空！');
                                                flag_list = false;
                                                return;
                                            }
                                            if(v.GWGD007 === null || v.GWGD007 === '' || v.GWGD007 === undefined){
                                                app.showWarning('数量不能为空！');
                                                flag_list = false;
                                                return;
                                            }
                                            if(v.GWGD008 === null || v.GWGD008 === '' || v.GWGD008 === undefined){
                                                app.showWarning('预算单价不能为空！');
                                                flag_list = false;
                                                return;
                                            }
                                        });
                                    }
                                    if(flag_list) {
                                        if (vueData.isAdd) {
                                            let flag = true;
                                            if (pageVue.$refs.uploadFile.uploadFiles.length > 0) {
                                                flag = false;
                                                pageVue.$refs.uploadFile.submit();
                                            }else if(vueData.form.GWGC029 != ""){
                                                flag = true;
                                            }else {
                                                flag = false;
                                                app.showWarning("附件不能为空");
                                                vueData.form.GWGC029 = "";
                                                return;
                                            }
                                            if (flag) {
                                                if(flag_list){
                                                    self.addData(listData);
                                                }
                                            }
                                        } else {
                                            if(pageVue.$refs.uploadFile.uploadFiles.length === 0 &&
                                                vueData.basicInfo_fileList2.length === 0) {
                                                app.showWarning("附件不能为空");
                                                vueData.form.GWGC029 = '';
                                                return;
                                            }
                                            vueData.form.GWGC029 = '';
                                            // 将文件链接拼接起来
                                            let gwgc029s = [];
                                            for (var i = 0; i < vueData.basicInfo_fileList2.length; i++) {
                                                gwgc029s[i] = vueData.basicInfo_fileList2[i].GWGC029;
                                            }
                                            vueData.form.GWGC029 = gwgc029s.join(",");

                                            // 将没有改动过的文件从上传列表中删掉
                                            vueData.basicInfo_fileList2.forEach(function (v) {
                                                pageVue.$refs.uploadFile.uploadFiles.removeByMap({
                                                    name: v.GWGC029
                                                });
                                            });

                                            let flag = true;
                                            if (pageVue.$refs.uploadFile.uploadFiles.length > 0) {
                                                flag = false;
                                                pageVue.$refs.uploadFile.submit();
                                            }
                                            if (flag) {
                                                if(flag_list){
                                                    self.editData(listData);
                                                }
                                            }
                                        }
                                    }
                                }else {
                                    var listData = gwgdTable.getDatas();
                                    var gwdelistData = gwgeTable.getDatas();
                                    self.addGwgeData(listData, gwdelistData);
                                }
                            }
                        });
                    },
                    beforeUploadFiles: function (file) {
                        const isJPG = (file.type === 'application/pdf'
                            || file.type === 'application/msword' || file.type === 'application/vnd.ms-excel'
                            || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                            || file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
                            || file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
                        const isLt2M = file.size / 1024 / 1024 < 5;
                        if (!isJPG) {
                            this.$message.error('文件格式不对');
                        }
                        if (!isLt2M) {
                            this.$message.error('文件大小不对');
                        }
                        return isJPG && isLt2M;
                    },
                    successUploadFiles: function (response) {
                        if (response.code == 200) {
                            pageVue.$message.success('文件上传成功');
                            vueData.form.GWGC009 = response.data[0].name;
                            var listData = gwgdTable.getDatas();
                            if (vueData.isAdd) {
                                self.addData(listData);
                            } else {
                                self.editData(listData);
                            }
                            pageVue.$refs.upload2.clearFiles();
                        }
                    },
                    handleFileRemove: function (file, fileList) {
                        vueData.form.GWGC009 = "";
                        file = '';
                    },
                    handleFilePreview: function (file) {
                        window.open('/smfile/' + file.name);
                    },
                    handleExceed: function (files, fileList) {
                        this.$message.warning("当前只允许选择一个文件，如果需要选择新的文件，请先删除之前的文件!");
                    },

                    // 基本信息附件上传方法
                    handleBasicInfoFilePreview: function (file) {
                        window.open('/smfile/' + file.name);
                    },
                    handleBasicInfoFileRemove: function (file, fileList) {
                        if (!vueData.isAdd) {
                            vueData.basicInfo_fileList2.removeByMap({
                                GWGC029: file.name
                            });
                        }
                    },
                    beforeUploadBasicInfoFiles: function (file) {
                        const isJPG = (file.type === 'application/pdf'
                            || file.type === 'text/plain'
                            || file.type === 'application/msword' || file.type === 'application/vnd.ms-excel'
                            || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                            || file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
                            || file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
                        const isLt10M = file.size / 1024 / 1024 < 10;
                        if (!isJPG) {
                            this.$message.error('文件格式不对');
                        }
                        if (!isLt10M) {
                            this.$message.error('文件最多为10M');
                        }
                        return isJPG && isLt10M;
                    },
                    successUploadBasicInfoFiles: function (response) {
                        if (response.code === 200) {
                            if (vueData.form.GWGC029 === '' || vueData.form.GWGC029 == null) {
                                vueData.form.GWGC029 = response.data[0].name;
                            } else {
                                vueData.form.GWGC029 = vueData.form.GWGC029 + ',' + response.data[0].name;
                            }

                            vueData.fileCount++;
                            if (vueData.fileCount === pageVue.$refs.uploadFile.uploadFiles.length) {
                                pageVue.$message.success('文件上传成功');
                                let listData = gwgdTable.getDatas();
                                if (vueData.isAdd) {
                                    self.addData(listData);
                                } else {
                                    self.editData(listData);
                                }
                                pageVue.$refs.uploadFile.clearFiles();
                            }
                        }
                    },

                    addGwgd:function () {
                        var choose_data = gwgdTable.getDatas();
                        var count = 0;
                        if(choose_data.length > 0) {
                            count = choose_data[choose_data.length - 1].GWGD002;
                        }
                        choose_data.push({
                            GWGD002: count+1,
                            GWGD003: '',
                            GWGD004: '',
                            GWGD005: '',
                            GWGD006: '',
                            GWAML003: '',
                            GWAHL003: '',
                            GWAOL003: '',
                            GWGD007: '',
                            GWGD008: '',
                            GWGD009: '',
                            GWGD010: ''
                        });
                        self.gwgdTableReload(choose_data);
                    },
                    delGwgd:function () {
                        var listData = gwgdTable.getDatas();
                        var checkStatus = table.checkStatus('gwgi002_gwgd_table');
                        if (checkStatus.data.length > 0) {
                            checkStatus.data.forEach(function (v) {
                                listData.removeByMap({
                                    GWGD002: v.GWGD002
                                });
                            });
                            if (checkStatus.length != 0) {
                                var i = 1;
                                listData.forEach(function (v) {
                                    v.GWGD002 = i;
                                    i++;
                                });
                            }
                            self.gwgdTableReload(listData);
                        }
                    },
                    addGwge:function () {
                        var choose_data = gwgeTable.getDatas();
                        var count = 0;
                        if(choose_data.length > 0) {
                            count = choose_data[choose_data.length - 1].GWGE002;
                        }
                        choose_data.push({
                            GWGE002: count+1,
                            GWGE003: '',
                            /*GWGE004: '',*/
                            GWGE005: '',
                            /*GWAML004: '',*/
                            GWAHL003: '',
                            GWAOL003: '',
                            GWGE006: '',
                            GWGE007: '',
                            GWGE008: '',
                            GWGE009: '',
                            GWGE010: 'N'
                        });
                        self.gwgeTableReload(choose_data);
                    },
                    delGwge:function () {
                        var listData = gwgeTable.getDatas();
                        var checkStatus = table.checkStatus('gwgi002_gwge_table');
                        if (checkStatus.data.length > 0) {
                            checkStatus.data.forEach(function (v) {
                                listData.removeByMap({
                                    GWGE002: v.GWGE002
                                });
                                self.getGwdc016(v, '');
                            });
                            if (checkStatus.length != 0) {
                                var i = 1;
                                listData.forEach(function (v) {
                                    v.GWGE002 = i;
                                    i++;
                                });
                            }
                            self.gwgeTableReload(listData);
                        }
                    },
                    gwgc014Change:function () {
                        var gwgeData = gwgeTable.getDatas();
                        var gwgdData = gwgdTable.getDatas();
                        if(vueData.form.GWGC014 == 'Y'){
                            gwgdTable.config.cols[0][10].edit = 'text';
                            gwgeTable.config.cols[0][10].edit = false;
                        }else{
                            gwgdTable.config.cols[0][10].edit = false;
                            gwgeTable.config.cols[0][10].edit = 'select';
                        }
                        if(gwgeData != undefined && gwgeData.length > 0) {
                            gwgeData.forEach(function (v) {
                                v.GWGE010 = 'N';
                            });
                            self.gwgeTableReload(gwgeData);
                        }
                        if(gwgdData != undefined && gwgdData.length > 0) {
                            gwgdData.forEach(function (v) {
                                v.GWGD010 = '';
                            });
                            self.gwgdTableReload(gwgdData);
                            vueData.form.GWGC016 = '';
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwgi002_edit');
                },
                computed:{
                    rulesList:function () {
                        if(vueData.isAddGwde){
                            return vueData.rules2;
                        }else {
                            return vueData.rules1;
                        }
                    }
                },
                watch: {
                    'form.GWGC024':function(newValue) {
                        if(newValue == null) return;
                        let GWGC002 = vueData.form.GWGC002;
                        if( self.compareDate(newValue,GWGC002)) {
                            app.showWarning("开标时间必须大于等于保证金截止时间");
                            vueData.form.GWGC024 = null;
                        }
                    },
                    'form.GWGC002':function(newValue) {
                        if(newValue == null) return;
                        let GWGC024 = vueData.form.GWGC024;
                        if( self.compareDate(GWGC024,newValue)) {
                            app.showWarning("开标时间必须大于等于保证金截止时间");
                            vueData.form.GWGC002 = null;
                        }
                    },
                    'form.GWGC004': function () {
                        if(vueData.form.GWGC004 == 'Y'){
                            vueData.gwgc004Disabled = false;
                        }else if(vueData.form.GWGC004 == 'N'){
                            vueData.form.GWGC005_NAME = '';
                            vueData.form.GWGC005 = '';
                            vueData.form.GWGC006 = 'N';
                            vueData.gwgc004Disabled = true;
                        }
                    },
                    'form.GWGC005_NAME': function () {
                        if(vueData.form.GWGC005_NAME == ''){
                            vueData.GWGC005 = '';
                        }
                    }
                }
            });

            self.initGwgdTable();
            self.initGwgeTable();
        },

        _lazy: function () {
            this._super();

            app.ajax({
                xurl: 'admin/getDigit',
                cb: function (result) {
                    if(result.ok) {
                        var data = result.data.data;
                        vueData.AMOUNT = Math.pow(10,data.amount);
                    }else {
                        console.log("获取小数位失败");
                    }
                }
            });
        }
    });

    return new Gwgi002Edit();

});